/*
 * Copyright [2012] Gurkan Erdogdu
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.   
*/  
package com.google.code.miniwebserver;

import org.apache.coyote.http11.Http11Protocol;

/**
 * Real HTTP Server.
 * 
 * @author gurkanerdogdu
 *
 */
public class Server {
    
    /**BIO Protocol*/
    private final Http11Protocol protocolHandler;
    
    /**
     * Creates a new server.
     * @param protocolHandler protocol handler
     * @param port port
     */
    public Server(String protocolHandler, Integer port){
        // Instantiate protocol handler
        try {
            Class<?> clazz = Class.forName(protocolHandler);
            this.protocolHandler = (Http11Protocol) clazz.newInstance();
            this.protocolHandler.setAdapter(new FileAdapter());
            this.protocolHandler.setPort(port);
            
        } catch (Exception e) {
            throw new RuntimeException("Unable to create Protocol Handler : " + protocolHandler);
        }
    }
    
    /**
     * Initialize server.
     * @throws Exception for exception
     */
    public void init() throws Exception{
        this.protocolHandler.init();
    }
    
    /**
     * Starts server.
     * @throws Exception for exception
     */
    public void start() throws Exception{
        this.protocolHandler.start();
    }
    
    /**
     * Pause the server.
     * @throws Exception for exception
     */
    public void pause() throws Exception{
        this.protocolHandler.pause();
    }
    
    /**
     * Stops the server.
     * @throws Exception for exception.
     */
    public void stop() throws Exception{
        this.protocolHandler.stop();
    }
}
